/**
 * @Project Name:blog-business-service
 * @Package Name:cn.hoofungson.blog.business.service.provider.service.loginLog
 * @Since JDK 1.8
 */

package cn.hoofungson.blog.business.service.provider.service.user;

import cn.hoofungson.blog.business.service.interfaces.domain.dto.user.LoginLogDTO;
import cn.hoofungson.blog.business.service.provider.converter.user.LoginLogConverter;
import cn.hoofungson.blog.business.service.provider.model.entity.user.LoginLogPO;
import cn.hoofungson.blog.business.service.provider.repositories.user.LoginLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Description: LoginLogService
 * @Author 胡松 hoofungson@163.com
 * @Date 2020-03-25 8:30
 * @Version V1.0
 */
@Service
public class LoginLogService {

    @Autowired
    private LoginLogRepository loginLogRepository;

    /**
     * 保存或更新
     *
     * @param loginLogDTO
     **/
    public void save(LoginLogDTO loginLogDTO) {
        if (null == loginLogDTO) {
            return;
        }
        LoginLogPO loginLogPO = new LoginLogPO();
        LoginLogConverter.convert(loginLogDTO, loginLogPO);
        this.loginLogRepository.save(loginLogPO);
    }

    /**
     * 根据ID删除
     *
     * @param id
     **/
    public void deleteById(Long id) {
        this.loginLogRepository.deleteById(id);
    }

    /**
     * 根据所传实体删除
     *
     * @param loginLogDTO
     **/
    public void delete(LoginLogDTO loginLogDTO) {
        if (null == loginLogDTO) {
            return;
        }
        LoginLogPO loginLogPO = new LoginLogPO();
        LoginLogConverter.convert(loginLogDTO, loginLogPO);
        this.loginLogRepository.delete(loginLogPO);
    }

    /**
     * 删除所有
     **/
    public void deleteAll() {
        this.loginLogRepository.deleteAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     **/
    public LoginLogDTO findById(Long id) {
        Optional<LoginLogPO> optional = this.loginLogRepository.findById(id);
        if (!optional.isPresent()) {
            return null;
        }
        LoginLogPO loginLogPO = optional.get();
        LoginLogDTO loginLogDTO = new LoginLogDTO();
        LoginLogConverter.convert(loginLogPO, loginLogDTO);
        return loginLogDTO;
    }
}
